<template>
  <div class="wx-h5">
    <div style="color: #1989fa;" vertical>正在授权登录中，请稍后...</div>
  </div>
</template>
<script lang="ts" setup>
// import { showSuccessToast } from 'vant';
import { onMounted } from 'vue'
import { useStore } from 'vuex';
import { wechatLogin } from '@/api/api.js'
const store = useStore()
const getCode = () => {
  const code = getUrlParam('code');
  if (code) {
    store.dispatch('GetLoginDatas', { code: code, type: 'H5' })
      .then(res => {
        history.go(-2);
      })
      .catch(error => {
        console.error('Auth Failed:', error); // 错误捕获
      });
  } else {
    const local = encodeURIComponent(window.location.href);
    window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa1b5acb99de626aa&redirect_uri=${local}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`;
  }
};
const getUrlParam = (name: string) => {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
  var r = window.location.search.substr(1).match(reg)
  if (r != null) {
    return unescape(r[2])
  } else {
    return null
  }
}
onMounted(() => {
  if (localStorage.getItem('accessToken')) {
    history.go(-1);
    return
  }
  getCode()
})
</script>

<style lang="scss">
.wx-h5 {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.1);
}
</style>
